<template>
  <div class="_page">
    <div class="content">
      <div class="left">
        <el-image style="width: 100px; height: 50px" :fit="fit" />
        <h1>管理系统</h1>
        <p>
          项目简介项目简介项目简介项目简介项目简介项目简介项目简介
          项目简介项目简介项目简介项目简介项目简介项目简介项目简介
          项目简介项目简介项目简介项目简介项目简介项目简介项目简介
          项目简介项目简介项目简介项目简介项目简介项目简介项目简介
        </p>
      </div>
      <div class="right">
        <h1>登录</h1>
        <el-form :model="loginForm" :rules="rules">
          <el-form-item prop="username">
            <input v-model="loginForm.username" placeholder="账号" />
          </el-form-item>
          <el-form-item prop="password">
            <input
              v-model="loginForm.password"
              type="password"
              placeholder="密码"
            />
          </el-form-item>
          <button @click="login" :disabled="loading">登录</button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useUserStore } from '@/store/modules/user'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

// 可以在组件中的任意位置访问 `store` 变量 ✨
const userStore = useUserStore()

let $router = useRouter()
let loading = ref(false)

let loginForm = reactive({ username: 'admin', password: '123456' })
const rules = {
  username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
}

const login = async () => {
  let ret = false

  Object.keys(loginForm).forEach((key) => {
    if (!loginForm[key]) {
      ret = true
    }
  })

  if (ret) {
    ElMessage({
      type: 'warning',
      message: '请完善登录信息',
    })
    return false
  }

  loading.value = true
  try {
    await userStore.login(loginForm)
    $router.push('/guide')
    loading.value = false
  } catch (error) {
    loading.value = false
  }
}
</script>

<style lang="scss" scoped>
._page {
  position: relative;
  width: 100vw;
  height: 100vh;
}

.content {
  display: flex;
  justify-content: space-between;

  position: absolute;
  left: 50%;
  top: 50%;
  width: 850px;
  transform: translate(-50%, -50%);
  box-shadow: 0px 20px 80px 0px rgba(0, 0, 0, 0.3);

  h1 {
    font-size: 1.7rem;
  }

  .left,
  .right {
    flex: 1;
    padding: 80px 48px 80px;
  }

  .left {
    background: linear-gradient(0deg, #3a485a 0%, #607089 100%);
    color: #ffffff;

    h1 {
      margin-top: 10px;
    }

    p {
      line-height: 1.7;
      margin-top: 10px;
    }
  }

  .right {
    h1 {
      margin-bottom: 15px;
    }

    input,
    button {
      width: 100%;
      height: 40px;
      border: none;
    }

    input {
      border: 1px solid #dfe3e9;
      padding: 0.678807em 1em;
      border-radius: 5px;

      &:focus {
        border: 1px solid #fe8700;
      }
    }

    button {
      color: #fff;
      background: #fe7300;
      cursor: pointer;

      &:hover {
        opacity: 0.9;
      }

      &:active {
        background: #fe5d00;
      }

      &:disabled {
        background: #f8955c;
        cursor: not-allowed;
      }
    }
  }
}
</style>
